<?php

class FotoC extends CActiveRecord {
    /**
     * The followings are the available columns in table 'z_foto_cat':
     * @var integer $id_foto_cat
     * @var string $name
     */

    /**
     * Returns the static model of the specified AR class.
     * @return CActiveRecord the static model class
     */
    public static function model($className=__CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'z_foto_cat';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
                array('name', 'required'),
                // The following rule is used by search().
                // Please remove those attributes that should not be searched.
                array('id_foto_cat, name', 'safe', 'on'=>'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
                'id_foto_cat' => 'id',
                'name' => 'Название',
        );
    }


    /*
     * @return var name fotoalbom
    */
    public function getName($id) {
        $model = FotoC::model()->findByPk($id);
        return $model->name;
    }
    /*
     * @return array for dropList
    */
    public static function getAllItem() {
        $model = FotoC::model()->findAll();
        return $model;
    }

    public function delAll($id_cat) {

        //find all post(for deleted comments)
        $post = Foto::model()->findAll(
                array(
                'condition'=>'id_foto_cat=:id_cat_post',
                'params'=>array(':id_cat_post'=>$id_cat),
        ));
        //delete all post
        Foto::model()->deleteAll(
                array(
                'condition'=>'id_foto_cat=:id_cat_post',
                'params'=>array(':id_cat_post'=>$id_cat),
        ));
        //delete all fotos this category
        for($a = 0; $a < count($post); $a ++)
            if(file_exists(dirname(__FILE__).'/../../media/foto/'.$post[$a]->link)) unlink(dirname(__FILE__).'/../../media/foto/'.$post[$a]->link);
        if(file_exists(dirname(__FILE__).'/../../media/foto/preview/'.$post[$a]->link)) unlink(dirname(__FILE__).'/../../../media/foto/preview/'.$post[$a]->link);
        //delete all coments
        for($a = 0; $a < count($post); $a ++)
            Coments::model()->deleteAll(
                    array(
                    'condition'=>'id_name=:id_name AND id_post=:id_post',
                    'params'=>array(
                            ':id_name'=>'foto',
                            ':id_post'=>$post[$a]->id_foto),
            ));
        return true;
    }
    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria=new CDbCriteria;

        $criteria->compare('id_foto_cat',$this->id_foto_cat);

        $criteria->compare('name',$this->name,true);

        return new CActiveDataProvider('FotoC', array(
                        'criteria'=>$criteria,
        ));
    }
}